﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3
{
    public partial class DriverSelectionForm : Form
    {
          private String connString = Program.ConnStr;
        public DriverSelectionForm()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
            clearcontrol();
            
        }

        private void DriverSelectionForm_Load(object sender, EventArgs e)
        {
            showdataalldriverassignment();
            combojobcodeboxload();
            comboVehicleNoload();
        }


        /// <summary>
        /// load the jobcode in combobox
        /// </summary>

        public void combojobcodeboxload()
        {

            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();
            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobcode from  jobcodemastertable", oleDbConnection1);
            OleDbDataReader reader = oleDbCommand1.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("jobpk", typeof(int));
            dt.Columns.Add("jobcode", typeof(string));
            dt.Load(reader);
            cmbjobcode.ValueMember = "jobpk";
            cmbjobcode.DisplayMember = "jobcode";
            cmbjobcode.DataSource = dt.DefaultView;

            oleDbConnection1.Close();

        }


        /// <summary>
        ///load Vehicle details on page load
        ///
        /// </summary>

        public void comboVehicleNoload()
        {

            try
            {
                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                oleDbConnection1.Open();

                OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select driverid ,drivername , contactno , vehicleno from  drivermastertable ", oleDbConnection1);



                OleDbDataReader reader = oleDbCommand1.ExecuteReader();


                DataTable dt = new DataTable();
                dt.Columns.Add("driverid", typeof(int));
                dt.Columns.Add("drivername", typeof(string));
                dt.Columns.Add("contactno", typeof(string));
                dt.Columns.Add("vehicleno", typeof(string));
                dt.Load(reader);
                if (dt.Rows.Count != 0)
                {
                    cmbVehicleno.ValueMember = "driverid";
                    cmbVehicleno.DisplayMember = "vehicleno";
                    cmbVehicleno.DataSource = dt.DefaultView;
                    txtdriverName.Text = dt.Rows[0]["drivername"].ToString();
                    txtmobileno.Text = dt.Rows[0]["contactno"].ToString();

                }
                else
                {
                    cmbVehicleno.Text = "";
                    txtdriverName.Text = "";
                         txtmobileno.Text="";
                }

                oleDbConnection1.Close();
            }
            catch (Exception exp)
            {

                MessageBox.Show("this application has met with an error and need to restart Pls close all acess databases before restarting >>> at  comboVehicleNoload " + exp + "");
            }

        }
        /// <summary>
        /// load the mobileno and vehicleno when drivername selected in combo box
        /// in textboxes
        /// 
        /// </summary>

        public void datacombochange()
        {


            try
            {
                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                oleDbConnection1.Open();

                OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select driverid ,drivername , contactno , vehicleno from  drivermastertable where driverid = @driveridvalue", oleDbConnection1);

                oleDbCommand1.Parameters.AddWithValue("@driveridvalue", cmbVehicleno.SelectedValue);

                OleDbDataReader reader = oleDbCommand1.ExecuteReader();


                DataTable dt = new DataTable();
                dt.Columns.Add("driverid", typeof(int));
                dt.Columns.Add("drivername", typeof(string));
                dt.Columns.Add("contactno", typeof(string));
                dt.Columns.Add("vehicleno", typeof(string));
                dt.Load(reader);
                if (dt.Rows.Count != 0)
                {
                    // cmbDrivername.ValueMember = "driverid";
                    //  cmbDrivername.DisplayMember = "drivername";
                    //   cmbDrivername.DataSource = dt.DefaultView;
                    txtdriverName.Text = dt.Rows[0]["drivername"].ToString();
                    txtmobileno.Text = dt.Rows[0]["contactno"].ToString();



                    oleDbConnection1.Close();
                }
                else
                {
                    MessageBox.Show("NO SUCH DRIVER");
                   
                }
            }
            catch (Exception exp)
            {

                MessageBox.Show("this application has met with an error and need to restart Pls close all acess databases before restarting  >>at  datacombochange(" + exp + "");
            }

        }

        /// <summary>
        /// when drivernamecombobox value changers fills the vehicleno
        /// and mobileno automatically
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbVehicleno_SelectedIndexChanged(object sender, EventArgs e)
        {
            datacombochange();

        }


        /// <summary>
        /// clear controlls
        /// </summary>
        public void clearcontrol()
        {

            txtmobileno.Text = "";
            txtdriverName.Text = "";
            txtamount.Text = "";
            dtpdateoftrip.Value = DateTime.Now;

        }

        private void btnsubmit_Click(object sender, EventArgs e)
        {

            if (txtamount.Text.Trim() == null || txtamount.Text.Trim() == "")
            {

                txtamount.Focus();
                lblstatus.Text = "Enter an valid Amount";
                txtamount.Text = "";
            }
            else
            {
                try
                {
                   
                    Int32 confirmedamount = Int32.Parse(txtamount.Text);

                    try
                    {
                        OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                        oleDbConnection1.Open();
                        OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO driverassignmastertable (jobcode ,jobcodepk , drivername , vehicleno, mobileno , amount ,workdate,driverpk  ) Values ( @jobcode , @jobpk ,@drivername , @vehicleno, @mobileno  , @amount, @workdate , @driverpk ) ", oleDbConnection1);

                        oleDbCommand1.Parameters.AddWithValue("@jobcode", cmbjobcode.Text);
                        oleDbCommand1.Parameters.AddWithValue("@jobcodepk", cmbjobcode.SelectedValue);
                        oleDbCommand1.Parameters.AddWithValue("@drivername", txtdriverName.Text);
                        oleDbCommand1.Parameters.AddWithValue(" @vehicleno", cmbVehicleno.Text);
                        oleDbCommand1.Parameters.AddWithValue("@mobileno", txtmobileno.Text);
                        oleDbCommand1.Parameters.AddWithValue(" @amount", Int32.Parse(txtamount.Text));
                        oleDbCommand1.Parameters.AddWithValue(" @workdate", dtpdateoftrip.Value.Date);
                        oleDbCommand1.Parameters.AddWithValue("@driverpk", cmbVehicleno.SelectedValue);
                        oleDbCommand1.CommandType = CommandType.Text;


                        oleDbCommand1.ExecuteNonQuery();
                        MessageBox.Show("Sucess");
                        


                        oleDbConnection1.Close();
                        clearcontrol();
                        showdataalldriverassignment();
                    }
                    catch (Exception Exp)
                    {

                        MessageBox.Show("Error in database transaction>> at  btnsubmit_Click" + Exp + "");
                    }

                }
                catch (Exception)
                {
                    lblstatus.Text = "EnterThe Amount";
                    txtamount.Text = "";

                }

            }



            

        }



        public void showdataalldriverassignment()
        {
            try
            {

                String query = "Select * from driverassignmastertable";
                OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);
                DataSet ds = new DataSet();
                dAdapter.Fill(ds, "tbluserdataview");
                tbldriverassignment.DataSource = ds.Tables["tbluserdataview"].DefaultView;

            }
            catch (Exception exp)
            {
                MessageBox.Show("The application had met with some errors please restart  the application :\n error:closer MSAccess files>> at showdata" + exp + "");
                Console.WriteLine("Error", exp);
            }


        }












    }
}
